package com.itheima.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * AOP切面类
 */
@Slf4j
@Component
@Aspect
@Order(1)
public class TimeAspect {
    //第一个* 返回值
    //execution  表达式 第一个* 返回值 "第二个* 某一个类"."某一个方法"."方法参数"
    @Around("execution(* com.itheima.service.*.*(..))")//设置表达式 代理的类或方法
    public Object aop(ProceedingJoinPoint joinPoint) throws Throwable {
        //开始时间
        long l = System.currentTimeMillis();
        //调用原方法运行
        Object proceed = joinPoint.proceed();
        //获取结束时间
        long l1 = System.currentTimeMillis();

        String name = joinPoint.getSignature().getName();
        log.info("{}运行时间为{}",name,l1-l);
        return proceed;
    }

        @Before("execution(* com.itheima.service.*.*(..))")
        public void before(){
        log.info("前置通知");
        }

        @After("execution(* com.itheima.service.*.*(..))")
        private void after(){
        log.info("后置通知");
        }
        @Around("execution(* com.itheima.mapper.*.*(..))")//"某一个类"."某一个方法"."方法参数"
        public Object around(ProceedingJoinPoint pjt) throws Throwable {
            Object proceed = pjt.proceed(); //获取原方法
            return proceed;
        }

        @Around("@annotation(com.itheima.aspect.Aop)&&execution(* com.itheima.mapper.*.*(..))")
        public Object around2(ProceedingJoinPoint pjp) throws Throwable {
            Object obj = pjp.proceed();
            return obj;
        }













    @Around("execution(* com.itheima.controller.*.*(..))")
    public Object aop2(ProceedingJoinPoint pjp) throws Throwable {
        //方法运行前代码
        Object obj = pjp.proceed();
        //方法运行后代码
        return obj;
    }
}
